package com.lian.dao.impl;

import com.lian.dao.staffdao;
import com.lian.dao.userdao;
import com.lian.entity.staff;
import com.lian.util.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/8/6.
 */
public class staffdaoimpl implements staffdao
{
    @Override
    public List<staff> findstaff(int staffid) {
        List<staff>list=new ArrayList<staff>();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select*from staff where staffid=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, staffid);
            rs = stat.executeQuery();
            userdao dao=new userdaoimpl();
            while(rs.next()){
                staff st=new staff();
                st.setStaffid(rs.getInt("staffid"));
                st.setUserid(rs.getInt("userid"));
                st.setUid(dao.findus(rs.getInt("userid")));
                list.add(st);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return list;
    }

    //根据id查询
    public int findID(int uid) {
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        int id=-1;
        String sql="select staffid from staff where userid=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, uid);
            rs = stat.executeQuery();
            userdao dao=new userdaoimpl();
           if(rs.next()){
               id=rs.getInt("staffid");

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return id;
    }

    public int FindByStaffid(int staffid){
        String sql="SELECT uid,uname from user WHERE uid=(SELECT userid FROM staff WHERE staffid=(SELECT staffid FROM `work` WHERE workid=?))";
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
       int i=-1;
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, staffid);
            rs = stat.executeQuery();
            userdao dao=new userdaoimpl();
            if(rs.next()){
            i=rs.getInt(1);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return i;
    }
}
